package model.utils;

import controller.GameBoard;
import android.os.CountDownTimer;

/**
 * Counter which counts elapsed time before the next wave begins.
 * 
 * @author EvilTeam
 * 
 */
public class Counter {
	private static boolean isCountDownRunning = false;
	private static boolean isCountDownCanceled = false;

	public static void startCountDown(long millisInFuture) {

		if (isCountDownRunning == false) {
			new CountDownTimer(millisInFuture, 1000) {

				public void onTick(long millisUntilFinished) {
					if (isCountDownCanceled) {
						this.cancel();
					} else {
						GameBoard.getSeconds().setText(
								"" + millisUntilFinished / 1000);
					}
				}

				public void onFinish() {
					if (GameBoard.getSeconds() != null)
						GameBoard.getSeconds().setText("35");
					isCountDownRunning = false;
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
		isCountDownRunning = true;
	}

	public static void setCountDownCanceled(boolean isCountDownCanceled) {
		Counter.isCountDownCanceled = isCountDownCanceled;
	}
}
